<template>
  <el-dialog
    v-model="centerDialogVisible"
    title="添加客户信息"
    width="40%"
    center
    @close="closeDialog(false)"
  >
    <el-form
      ref="validateForm"
      :model="customerForm"
      :rules="rules"
      label-width="120px"
      :status-icon="true"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="customerForm.name"></el-input>
      </el-form-item>
      <el-form-item label="手机/电话" prop="phone">
        <el-input v-model="customerForm.phone"></el-input>
      </el-form-item>

      <el-form-item label="客户类型" prop="fromType">
        <el-select v-model="customerForm.fromType" placeholder="请选择"  @change="fromTypeChange">
          <el-option
            v-for="item in fromTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="经销商" prop="productId" v-if="dealerFlag===true">
       
        <el-select
            v-model="customerForm.ownDealerId" 
            filterable
            placeholder="选择经销商"
          >

          <el-option
            v-for="item in dealerList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item> 
   <el-form-item label="联系状态" prop="connetState">
        <el-select v-model="customerForm.connetState" placeholder="请选择">
          <el-option
            v-for="item in connetStates"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属区域" prop="ownArea">
        <el-cascader
          v-model="customerArea"
          placeholder="查询或选择区域"
          :options="areas"
          filterable
          :props="areasProps"
        />
      </el-form-item>
    
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="customerForm.address"></el-input>
      </el-form-item>
     
      <el-form-item label="身份证" prop="idcard">
        <el-input v-model="customerForm.idcard"></el-input>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="customerForm.remark"></el-input
      ></el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button
          @click="
            () => {
              closeDialog(false);
            }
          "
          >取消</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from "vue";
import { ElMessage } from "element-plus";
import { postRequest,getRequest } from "../../utils/api";
import region from "../../utils/region";

function customerAdd(state, validateForm, emit) {
  const submitForm = () => {
    validateForm.value.validate((valid) => {
      if (valid) {
        if (state.customerArea) {
          state.customerForm.ownArea = state.customerArea.toString();
        }
        postRequest("/sell/customer/", state.customerForm).then((res) => {
          if (res && res.code == 200) {
            validateForm.value.resetFields();
            emit("onCloseDialog", false, res.data);
          } else {
            ElMessage({
              showClose: true,
              message: res.message,
              type: "error",
            });
          }
        });
      }
    });
  };
  return { submitForm };
}

export default {
  components: {},
  props: {
    centerDialogVisible: Boolean,
  },

  setup(props, { emit }) {
    const validateForm = ref();
    const state = reactive({
      centerDialogVisible: props.centerDialogVisible,
      dealerFlag:false,
      customerArea: ["河北省", "邯郸市", "成安县"],
      dealerList:[],
      connetStates: [
        {
          value: 0,
          label: "待沟通",
        },
        {
          value: 1,
          label: "沟通中",
        },
        {
          value: 2,
          label: "已下单",
        },
        {
          value: 3,
          label: "战败",
        },{
          value: 4,
          label: "停机",
        },{
          value: 5,
          label: "无效信息",
        },
      ],
      fromTypes: [
        {
          value: 0,
          label: "直销",
        },
        {
          value: 1,
          label: "经销商销售",
        },
      ],
      customerForm: {
        name: "",
        phone: "",
        username: "",
        address: "",
        remark: "",
        connetState: 1,
        fromType: 0,
        ownArea: "",
        ownDealerId:'',
        idcard:''
      },
      areasProps: { children: "children", label: "name", value: "name" },
      areas: region
    });

    const rules = {
      name: [{ required: true, message: "请输入客户姓名", trigger: "blur" }],
      phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
    };

    const closeDialog = (visible, data) => {
      validateForm.value.resetFields();
      emit("onCloseDialog", visible, data);
      state.customerForm.password = "";
    };

    const fromTypeChange=(val)=>{
        if(val==1){
          state.dealerFlag=true ;
        }else{
          state.dealerFlag=false ;
          state.customerForm.ownDealerId='';
        }
    }

    onMounted(()=>{
      //加载供应商信息
        getRequest('/sell/dealer/listAll').then(response => {
              if (response) {
                state.dealerList = response.data;
              }
            })
    })
    return {
      ...toRefs(state),
      closeDialog,
      fromTypeChange,
      rules,
      ...customerAdd(state, validateForm, emit), //解构goodsAdd返回的结果  返回submitAdd对象函数 需要把富文本编辑器也传过去 这里省略了
      validateForm,
    };
  },
};
</script>

<style>
.el-form-item__error {
  padding-top: 0px;
}
.el-input__inner {
  height: 32px;
  line-height: 32px;
}
.el-form-item {
  margin-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 18px;
  color: #8c939d;
  width: 88px;
  height: 88px;
  line-height: 88px;
  text-align: center;
}
.avatar {
  width: 88px;
  height: 88px;
  display: block;
}
</style>